package com.wd.tech.activity;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.wd.tech.R;
import com.wd.tech.base.CustomTabEntityDemo;
import com.wd.tech.fragment.CommunityFragment;
import com.wd.tech.fragment.ConsultingServiceFragment;
import com.wd.tech.fragment.NewsFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private final List<Fragment> fragments = new ArrayList<>();

    private ArrayList<CustomTabEntity> customTabEntities = new ArrayList<>();
    private ViewPager vp;
    private CommonTabLayout clt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp = findViewById(R.id.vp);
        clt = findViewById(R.id.clt);
        fragments.add(new ConsultingServiceFragment());
        fragments.add(new NewsFragment());
        fragments.add(new CommunityFragment());
        CustomTabEntityDemo demo1 = new CustomTabEntityDemo("咨询", R.mipmap.common_tab_informatiion_s_hdpi, R.mipmap.common_tab_information_n_hdpi);
        CustomTabEntityDemo demo2 = new CustomTabEntityDemo("信息", R.mipmap.common_tab_message_s_hdpi, R.mipmap.common_tab_message_n_hdpi);
        CustomTabEntityDemo demo3 = new CustomTabEntityDemo("社区", R.mipmap.common_tab_community_s_hdpi, R.mipmap.common_tab_community_n_hdpi);
        customTabEntities.add(demo1);
        customTabEntities.add(demo2);
        customTabEntities.add(demo3);
        clt.setTabData(customTabEntities);
        //适配器
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });
        //  CommonTabLayout 监听器
        clt.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vp.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        //p的监听器
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                clt.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

}
